package com.springboot.project.common.service.impl;

import com.springboot.config.tool.ToolUtil;
import com.springboot.project.common.dao.RolemoduleDao;
import com.springboot.project.common.entity.Role;
import com.springboot.project.common.dao.RoleDao;
import com.springboot.project.common.entity.Rolemodule;
import com.springboot.project.common.node.ZTreeNode;
import com.springboot.project.common.service.RoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lby
 * @since 2018-05-06
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

    @Autowired
    RolemoduleServiceImpl rolemoduleService;

    @Override
    public List<Role> list(Map<String, Object> map) {
        return baseMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return baseMapper.count(map);
    }

    @Override
    public void setAuthority(Integer roleId, String ids) {
        baseMapper.deleteRolesById(roleId);
       // 添加新的权限
       for (Integer id : ToolUtil.toIntArray(ids)) {
            Rolemodule relation = new Rolemodule();
            relation.setRoleid(roleId);
            relation.setMenuid(id);
           rolemoduleService.insert(relation);
       }
    }

    @Override
    public boolean exit(Map<String, Object> params) {
        List<Role> roles = baseMapper.list(params);
        if(roles.size() > 0)
        {
            return true;
        }
        return false;
    }

    @Override
    public List<ZTreeNode> roleTree() {
        List<ZTreeNode> tree = baseMapper.roleTree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    @Override
    public List<Map<String, Object>> roleTableList(Map<String, Object> map) {
        return baseMapper.roleTableList(map);
    }
}
